//
// Created by shen on 7/12/2024.
//

#include "AVQueue.h"

AVQueue::AVQueue() {
    pthread_mutex_init(&mutePacket,NULL);
    pthread_cond_init(&condPacket,NULL);
}

int AVQueue::get(AVPacket *packet) {

    pthread_mutex_lock(&mutePacket);

    while(true){
        if (queuePacket.size()>0){
            AVPacket *avPacket=queuePacket.front();

            if (av_packet_ref(packet,avPacket)==0){
                queuePacket.pop();
            }



        }




    }

    pthread_mutex_unlock(&mutePacket);



}
int AVQueue::size() {
    int size=0;
    pthread_mutex_lock(&mutePacket);
    size=queuePacket.size();
    pthread_mutex_unlock(&mutePacket);
    return size;
}
AVQueue::~AVQueue(){
    clearAvpacket();
}

void AVQueue::clearAvpacket() {
    pthread_cond_signal(&condPacket);

    pthread_mutex_unlock(&mutePacket);

    while (!queuePacket.empty()){
        AVPacket *avPacket=queuePacket.front();
        queuePacket.pop();

        av_packet_free(&avPacket);
        av_free(avPacket);
        avPacket = NULL;
    }
    pthread_mutex_unlock(&mutePacket);
}



